﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using FirstFloor.Documents.Annotations.Storage;
using FirstFloor.Documents.Annotations;
using FirstFloor.Documents.Behaviors;
using System.Windows.Interactivity;
using FirstFloor.Documents;

namespace SampleBrowser.Common.Annotations
{
    /// <summary>
    /// Represents a hyperlink annotation.
    /// </summary>
    public class Hyperlink
        : TextAnnotation
    {
        /// <summary>
        /// Identifies the hyperlink annotation type.
        /// </summary>
        public static readonly XName TypeHyperlink = XName.Get("Hyperlink", AnnotationStore.NamespaceAnnotations);

        private Uri targetUri;

        /// <summary>
        /// Initializes a new instance of the <see cref="Hyperlink"/> class.
        /// </summary>
        public Hyperlink()
            : base(TypeHyperlink)
        {
        }

        /// <summary>
        /// Gets or sets the target uri.
        /// </summary>
        /// <value>The target URI.</value>
        public Uri TargetUri
        {
            get { return this.targetUri; }
            set
            {
                if (this.targetUri != value) {
                    this.targetUri = value;
                    OnPropertyChanged("TargetUri");
                }
            }
        }

        /// <summary>
        /// Renders the annotation for given glyphs element in specified rectangle.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="glyphs">The glyphs.</param>
        /// <param name="rectangle">The rectangle.</param>
        protected override void RenderGlyphsAnnotation(AnnotationRenderContext context, Glyphs glyphs, Rect rectangle)
        {
            // add transparent hotspot 
            var hotspot = new Rectangle() {
                Fill = new SolidColorBrush(Color.FromArgb(0x30, 0x0, 0x0, 0xff)),
                Width = rectangle.Width,
                Height = rectangle.Height,
                Cursor = Cursors.Hand
            };
            Canvas.SetLeft(hotspot, rectangle.Left);
            Canvas.SetTop(hotspot, rectangle.Top);
            ToolTipService.SetToolTip(hotspot, this.targetUri);

            // use behavior to handle navigation on click
            var behavior = new HyperlinkNavigationBehavior() {
                TargetUri = this.targetUri
            };
            behavior.Attach(hotspot);

            // render underline
            var line = new Line() {
                Stroke = new SolidColorBrush(Colors.Blue),
                StrokeThickness = 2,
                X1 = rectangle.Left,
                Y1 = rectangle.Bottom,
                X2 = rectangle.Right,
                Y2 = rectangle.Bottom
            };

            // ensure the hotspot is clickable
            context.AnnotationAdornerLayer.Children.Add(hotspot);
            context.AnnotationLayer.Children.Add(line);
        }
    }
}
